define([ 'jquery', 'bootstrap', 'select2', 'validate', 'validate.en',
		'koala-search', 'shiftform', 'modalSearch', 'datetimepicker',
		'datetimepickerCN', 'date', 'date.zh' ], function($, validate) {
	return {

		init : function() {
			this.initPage();
			this.initWarehouse();
			this.initClient();
			this.initVehicle();
			this.initManufacturer();
			this.triggerModalSearch();
			this.initCompany();

		},

		initPage : function() {

			$(document).find('form').bind('reset', function(e) {
				$(this).find(".select2").select2("val", "");
			})

			$(document).find('form').bt_validate();
			$('select[name="organizationId"]').select2({
				theme : "classic",
				placeholder : '请选择',
				minimumResultsForSearch : -1,
				allowClear : true,
			});

			$('select[name="transType"]').select2({
				theme : "classic",
				placeholder : '请选择',
				minimumResultsForSearch : -1,
				allowClear : true,
			});

			$('select[name="status"]').select2({
				theme : "classic",
				placeholder : '请选择',
				minimumResultsForSearch : -1,
				allowClear : true,
			});

			$('.date').datetimepicker({
				format : 'yyyy-mm-dd',
				autoclose : true,
				language : 'zh-CN',
				todayBtn : 'linked',
				startDate : '${today}',
				todayHighlight : true,
				pickerPosition : 'bottom-right',
				minView : 'month'
			});
			$('select[name="isOutsourcing"]').select2({
				theme : "classic",
				placeholder : '请选择',
				minimumResultsForSearch : -1,
				allowClear : true,
			});

		},

		initWarehouse : function() {

			$('[data-target="#modal-search-fromWarehouse"]').modalsearch({
				url : window.__CTX__.root + '/warehouses/query',
				tableColumns : [ '出发地代码', '出发地名称' ],
				tableValues : [ 'code', 'name' ],
				displayValues : [ 'code', 'name' ],
				values : [ 'id' ]
			});

			$('[data-target="#modal-search-toWarehouse"]').modalsearch({
				url : window.__CTX__.root + '/warehouses/query',
				tableColumns : [ '到达地代码', '到达地名称' ],
				tableValues : [ 'code', 'name' ],
				displayValues : [ 'code', 'name' ],
				values : [ 'id' ]
			});

			var options = {
				time : 1000,
				url : window.__CTX__.root + "/warehouses/query",
				queryName : "code",
				value : [ "id" ],
				display : [ "code", "name" ]
			}

			$('.fromWarehouse-search').koalaSearch(options);
			$('.toWarehouse-search').koalaSearch(options);

			$(document).on("koala.search.value.changed",
					".fromWarehouse-search, .toWarehouse-search", function() {
						console.log($(this).next().val());
						// TODO 其他查询后的逻辑
					})
		},

		initClient : function() {

			$('[data-target="#modal-search-client"]').modalsearch({
				url : window.__CTX__.root + '/clients/query',
				tableColumns : [ '客户代码', '客户名称' ],
				tableValues : [ 'code', 'name' ],
				displayValues : [ 'code', 'name' ],
				values : [ 'id' ]
			});

			var options = {
				time : 1000,
				url : window.__CTX__.root + "/clients/query",
				queryName : "code",
				value : [ "id" ],
				display : [ "code", "name" ]
			}

			$('.client-search').koalaSearch(options);
		},

		initVehicle : function() {

			$('[data-target="#modal-search-vehicle"]').modalsearch(
					{
						url : function() {
							var url = window.__CTX__.root + '/vehicles/query';
							return url + "?companyIds="
									+ $("[name='organizationId']").val();
						},
						tableColumns : [ '驳运车代码', '驳运车名称' ],
						tableValues : [ 'code', 'name' ],
						displayValues : [ 'code', 'name' ],
						values : [ 'id' ]
					});

			var options = {
				time : 1000,
				url : window.__CTX__.root + "/vehicles/query",
				queryName : "code",
				value : [ "id" ],
				display : [ "code", "name" ]
			}
			$('.vehicle-search').koalaSearch(options);

		},
		initCompany : function() {

			$('[data-target="#modal-search-company"]').modalsearch({
				url : window.__CTX__.root + '/transportation-companys/query',
				tableColumns : [ '外借公司代码', '外借公司名称' ],
				tableValues : [ 'code', 'name' ],
				displayValues : [ 'code', 'name' ],
				values : [ 'id' ]
			});

			var options = {
				time : 1000,
				url : window.__CTX__.root + "/transportation-companys/query",
				queryName : "code",
				value : [ "id" ],
				display : [ "code", "name" ]
			}

			$('.company-search').koalaSearch(options);
		},

		initManufacturer : function() {

			$('[data-target="#modal-search-manufacturer"]').modalsearch({
				url : window.__CTX__.root + '/manufacturers/query',
				tableColumns : [ '制造商代码', '制造商名称' ],
				tableValues : [ 'code', 'name' ],
				displayValues : [ 'code', 'name' ],
				values : [ 'id' ]
			});

			var options = {
				time : 1000,
				url : window.__CTX__.root + "/manufacturers/query",
				queryName : "code",
				value : [ "id" ],
				display : [ "code", "name" ]
			}

			$('.manufacturer-search').koalaSearch(options);
		},

		triggerModalSearch : function() {
			$('.modal-search').each(function() {
				var value = $(this).val();
				console.info(value);
				if (value) {
					$(this).trigger('blur');
				}
			});
		}

	}
})